
<div class="layui-fluid layui-anim febs-anim" id="select-user">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="user-table-form" id="selectUserForm">
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                            <legend style="font-size: 15px; font-weight: bolder;">批量查询</legend>
                        </fieldset>
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">部门</label>
                                        <div class="layui-input-inline" id="dept">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">岗位</label>
                                        <div class="layui-input-inline">
                                            <select name="postId" id="choPosition">
                                                <option value="">请选择</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                            <legend style="font-size: 15px; font-weight: bolder;">精准查询</legend>
                        </fieldset>
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">姓名</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="username" id="txtName" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">手机号</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="mobile" id="txtMobile" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain table-action" id="btnAccurateSearch">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain table-action" id="btnAccurateReset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="userTable" lay-data="{id: 'userTable'}"></table>
                </div>

                <div style="text-align:center" class="layui-form-item febs-hide">
                    <input type="button" class="layui-btn" lay-submit="" lay-filter="select-btnCheckResults-submit" id="btnCheckResults" value="选择勾选结果">
                    <input type="button" class="layui-btn" lay-submit="" lay-filter="select-btnPageResults-submit" id="btnPageResults" value="选择本页结果">
                    <input type="button" class="layui-btn" lay-submit="" lay-filter="select-btnAllResults-submit" id="btnAllResults" value="选择全部结果">
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="user-sex">
    {{#
    let sex = {
    2: {title: '保密'},
    1: {title: '女'},
    0: {title: '男'}
    }[d.sex];
    }}
    <span>{{ sex.title }}</span>
</script>

<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs', 'xmSelect'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#select-user'),
            $btnBatchSearch = $view.find('#btnBatchSearch'),
            $btnBatchReset = $view.find('#btnBatchReset'),
            $btnAccurateReset = $view.find('#btnAccurateReset'),
            $btnAccurateSearch = $view.find('#btnAccurateSearch'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo,
            deptXmlSelect;

        form.render();

        initTable();
        deptXmlSelect = xmSelect.render({
            el: '#dept',
            model: {label: {type: 'text'}},
            tree: {
                show: true,
                strict: false,
                showLine: false,
                clickCheck: true,
                expandedKeys: [-1],
            },
            name: 'deptId',
            theme: {
                color: '#52c41a',
            },
            prop: {
                value: 'id'
            },
            height: 'auto',
            on: function(data){
                if(data.isAdd){
                    return data.change.slice(0, 1)
                }
            }
        });
        febs.get(ctx + 'dept/select/tree', null, function (data) {
            deptXmlSelect.update(
                data
            )
        });

        //岗位的下拉菜单
        $.ajax({
            type:'get',
            url:'post/list',
            cache:false,
            success:function (res) {
                var postArray=res.data.rows
                if(postArray.length>0){
                    for (var i=0;i<postArray.length;i++){
                        var postone=postArray[i];
                        $("#choPosition").append("<option value='"+postone.postId+"'>"+postone.postName+"</option>")
                    }
                }
                layui.form.render("select");
            }
        });

        table.on('sort(userTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        //批量查询
        $btnBatchSearch.on('click', function () {
            let params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });


        //批量重置
        $btnBatchReset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            deptXmlSelect.setValue(['']);
            tableIns.reload({where: getQueryParams(true), page: {curr: 1}, initSort: sortObject });
        });

        //精准查询
        $btnAccurateSearch.on('click',function () {
            let params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        })

        //精准重置
        $btnAccurateReset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            deptXmlSelect.setValue(['']);
            tableIns.reload({where: getQueryParams(true), page: {curr: 1}, initSort: sortObject});
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'userTable',
                url: ctx + 'user/selectUserlist',
                cols: [[
                    {field: 'userId',type: 'checkbox'},
                    {field: 'userId', title: '编号', minWidth: 50},
                    {field: 'username', title: '姓名', minWidth: 100},
                    {title: '性别', templet: '#user-sex'},
                    {field: 'mobile', title: '电话', minWidth: 165},
                    {field: 'emergencryCall', title: '紧急联系人', minWidth: 165},
                    {field: 'email', title: '邮箱', minWidth: 180},
                    {field: 'deptName', title: '部门',minWidth: 100},
                    {field: 'postName', title: '岗位', minWidth: 180},
                ]],
            });
        }

        function getQueryParams(flag) {
            deptId = deptXmlSelect.getValue('valueStr');
            var post = $("#choPosition").val();
            var name = $("#txtName").val();
            var mobile = $("#txtMobile").val();
            if (flag) {
                deptId = '';
            }
            let params = $searchForm.serializeJson();
            params.postId=post;
            params.deptId = deptId;
            params.username=name;
            params.mobile=mobile;
            let createTime = params.time;
            if (createTime) {
                createTimeFrom = createTime.split(' - ')[0];
                createTimeTo = createTime.split(' - ')[1];
            }
            params.invalidate_ie_cache = new Date();
            params.createTimeFrom = createTimeFrom;
            params.createTimeTo = createTimeTo;
            return params;
        }

        //选择勾选状态
        $("#btnCheckResults").click(function () {
            var checkStatus=table.checkStatus("userTable");
            var data=checkStatus.data;
            var check_id=[];
            if(data.length>0){
                for (var i=0;i<data.length;i++){
                    var check=data[i];
                    check_id.push(check.userId)
                    parent.layui.$("#ttName").val(check_id);
                    layer.closeAll();
                }
            }else{
                febs.alert.warn('至少选择一个员工');
            }
        })
        //选择本页结果
        $("#btnPageResults").click(function () {
            datas=layui.table.cache["userTable"];
            var page_id=[];
            if(datas.length>0){
                for (var i=0;i<datas.length;i++){
                    var page=datas[i];
                    page_id.push(page.userId);
                    parent.layui.$("#ttName").val(page_id);
                }
            }
        })

        //选择
        form.on('submit(select-btnAllResults-submit)', function (data) {
            console.log($('#selectUserForm').serialize())
            $.ajax({
                type:'get',
                url:'user/selectUser',
                cache:false,
                traditional:true,
                data:$('#selectUserForm').serialize(),
                success:function (res) {
                    var all_id=[];
                    if(res.data.length>0){
                        for(var i=0;i<res.data.length;i++){
                            var add=res.data[i];
                            all_id.push(add.userId)
                            parent.layui.$("#ttName").val(all_id);
                        }
                    }
                }
            });
        });

    })
</script>